iT邦幫忙

0

第39天~~又是JSON+動態增加按鈕不是用XML

Tzu 2022-02-11 22:23:261070 瀏覽
  • 分享至 

  • xImage
  •  

這篇的上一篇:https://ithelp.ithome.com.tw/articles/10283923

~又是JSON~~要把JSON練熟~
https://zh.wikipedia.org/wiki/JSON

這裡要用logcat取出JSON的方法:

先建立新的Android檔-

https://ithelp.ithome.com.tw/upload/images/20220211/20119035b9Ztc9ElQe.png
用logcat取出JSON的方法:

https://ithelp.ithome.com.tw/upload/images/20220211/20119035ulgVXx4pxk.png

package com.huang.json2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String json = String.format("{\"%s\":\"%s\"}","firstName","Ken");
        Log.d("Howard","Json"+json);
    }
}

使用Android內建套件
把錯誤"印"出來~

https://ithelp.ithome.com.tw/upload/images/20220211/20119035McsO7jPVIw.png

目前變這樣
https://ithelp.ithome.com.tw/upload/images/20220211/20119035DLAa1zvt9G.png

然後再來改內容
https://ithelp.ithome.com.tw/upload/images/20220211/20119035FVtyit2m3e.png

package com.huang.json2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String json = String.format("{\"%s\":\"%s\"}","firstName","Ken");
        Log.d("Howard","Json"+json);

        try {
            JSONObject obj =new JSONObject(json);
        } catch (JSONException e) {
            Log.e("Howard","JSONException"+e);
        }
    }
}


再來取出數字:

https://ithelp.ithome.com.tw/upload/images/20220211/20119035CVSddrcJQl.png

跑一下取出KEN

https://ithelp.ithome.com.tw/upload/images/20220211/20119035SfhdzQqgUQ.png

package com.huang.json2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String json = String.format("{\"%s\":\"%s\"}","firstName","Ken");
        Log.d("Howard","Json"+json);

        try {
            JSONObject obj =new JSONObject(json);

            String firstName = obj.getString("firstName");
            Log.d("Howard","firstName:"+firstName);

        } catch (JSONException e) {
            Log.e("Howard","JSONException"+e);
        }
    }
}

動態增加按鈕不是用XML

/images/emoticon/emoticon04.gif
開新檔案-
XML檔先改成-LinearLayout

https://ithelp.ithome.com.tw/upload/images/20220212/20119035g60jbfArF4.png

XML裡面只有放...

命名- containerView
https://ithelp.ithome.com.tw/upload/images/20220212/20119035M1GKqwUsyY.png

然後用JAVA檔-然後用JAVA檔-
/images/emoticon/emoticon21.gif

package com.huang.dynamic_ui;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout layout=findViewById(R.id.containerView);

        Button button = new Button(this);
        button.setText("存檔");
        layout.addView(button);

    }
}

https://ithelp.ithome.com.tw/upload/images/20220212/20119035jT33tGAuRK.png

就可以長出這樣~

然後再加入for迴圈

https://ithelp.ithome.com.tw/upload/images/20220212/20119035PmmomlSUri.png

package com.huang.dynamic_ui;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout layout=findViewById(R.id.containerView);

        Button button = new Button(this);
        button.setText("存檔");
        layout.addView(button);

        for(int i=1;i <=10;i ++){
            CheckBox checkBox = new CheckBox(this);
            checkBox.setText("Item:" + i);
            layout.addView(checkBox);

        }

        

    }
}

就長成這樣:
https://ithelp.ithome.com.tw/upload/images/20220212/20119035c8cU6stbMt.jpg

這篇的下一篇:https://ithelp.ithome.com.tw/articles/10283962


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言